define(function(require, exports, module) {
  'use strict';

  var Search = require('../../common/search/search');
  var DialogForm = require('../../common/form/dialogform');

  var opts = [
    '<a href="javascript:;" data-id="{{id}}" data-doc-order="{{docOrder}}" data-doc-subject="{{docSubject}}" data-role="edit">编辑</a>',
    '<a href="javascript:;" data-id="{{id}}" data-role="del">删除</a>'
  ];
  var search = new Search({
    url: 'categoryList',
    delUrl: 'categoryDel',
    keepHistory: false,
    dateCfg: {
      disabled: true
    },
    formCfg: {
      buttons: [{
        colspan: null,
        'class': 'pull-right',
        value: '<i class="fa fa-plus"></i> 添加分类',
        attrs: {
          type: 'button',
          'class': 'btn btn-default btn-info',
          'data-role': 'add'
        }
      }],

      events: {
        render: function() {}
      }
    },
    gridCfg: {
      columns: [{
        key: 'id',
        name: '序号'
      }, {
        key: 'docSubject',
        name: '分类名称'
      }, {
        name: '操作',
        template: opts.join('')
      }]
    },
    gridHelpers: {

    },
    events: {
      render: function() {

      }
    },
    delegates: {
      'click [data-role=edit]': function(e) {
        var dataset = e.currentTarget.dataset;
        new DialogForm({
          title: '修改分类',
          formData: {
            id: dataset.id
          },
          formOptions: {
            url: 'categorySave',
            data: {
              groups: getGroups(dataset)
            }
          }
        });
      },
      'click [data-role=add]': function(e) {
        new DialogForm({
          title: '添加分类',
          formOptions: {
            url: 'categorySave',
            data: {
              groups: getGroups()
            }
          }
        });

        return false;
      }
    }
  });

  function getGroups(data) {
    return [{
      label: '分类序号',
      attrs: {
        type: 'text',
        required: 'required',
        value: data ? data.docOrder : '',
        name: 'docOrder'
      }
    }, {
      label: '分类名称',
      attrs: {
        type: 'text',
        required: 'required',
        value: data ? data.docSubject : '',
        name: 'docSubject'
      }
    }];
  }

});
